///////////////////////////////////////////////////////////////
//
// FileName : KFile.cpp
// Creator : Chen SiHan
// Date : 2009-7-13 17:30:00
// Comment :  The implementation of KFile.h. 
//
///////////////////////////////////////////////////////////////

#include "../stdafx.h"
#include "KFile.h"
#include "../error.h"
using namespace KCS0025;

KFile::KFile(void)
{

}


KFile::~KFile(void)
{

}


HANDLE KFile::MyCreateFile(const CString szFilePath)
{
	HANDLE hCFile = CreateFile(szFilePath,
							   GENERIC_WRITE | GENERIC_READ,
							   FILE_SHARE_WRITE | FILE_SHARE_READ,
							   NULL,
							   CREATE_ALWAYS,
							   FILE_ATTRIBUTE_NORMAL,
							   NULL);
	if (INVALID_HANDLE_VALUE == hCFile)
	{
		return NULL;
	}
	return hCFile;
}


HANDLE KFile::MyOpenFile(const CString szFilePath, DWORD *dwFileSize)
{
	HANDLE hOFile =  CreateFile(szFilePath,
								GENERIC_WRITE | GENERIC_READ,
								FILE_SHARE_WRITE | FILE_SHARE_READ,
								NULL,
								OPEN_EXISTING,
								FILE_ATTRIBUTE_NORMAL,
								NULL);
	if (INVALID_HANDLE_VALUE == hOFile)
	{
		return NULL;
	}
	*dwFileSize = GetFileSize(hOFile, NULL);
	return hOFile;
}


bool KFile::MyReadFile(HANDLE hOFile, char *pchContext, DWORD dwLength, DWORD dwReadSize)
{
	PROCESS_ERROR(hOFile != INVALID_HANDLE_VALUE);
	bool bDone = false;
	bDone = ReadFile(hOFile,
					 pchContext,
					 dwLength,
					 &dwReadSize,
					 NULL);
	DWORD err = GetLastError();
	if (!bDone)
	{
		PROCESS_ERROR(false);
	}
Exit0:
	return bDone;
}


bool KFile::MyReadFile(HANDLE hOFile, DWORD *pdwContext, DWORD dwLength, DWORD dwReadSize)
{
	PROCESS_ERROR(hOFile != INVALID_HANDLE_VALUE);
	bool bDone = false;
	bDone = ReadFile(hOFile,
		pdwContext,
		dwLength,
		&dwReadSize,
		NULL);
	DWORD err = GetLastError();
	if (!bDone)
	{
		PROCESS_ERROR(false);
	}
Exit0:
	return bDone;
}


bool KFile::MyReadFile(HANDLE hOFile, FILETIME *pdwContext, DWORD dwLength, DWORD dwReadSize)
{
	PROCESS_ERROR(hOFile != INVALID_HANDLE_VALUE);
	bool bDone = false;
	bDone = ReadFile(hOFile,
		pdwContext,
		dwLength,
		&dwReadSize,
		NULL);
	DWORD err = GetLastError();
	if (!bDone)
	{
		PROCESS_ERROR(false);
	}
Exit0:
	return bDone;
}


bool KFile::MyWriteFile(HANDLE hCFile, char *pchContext, DWORD dwLength)
{
	bool bDone = false;
	DWORD dwReadSize = 0;
	WriteFile(hCFile,
			  pchContext,
			  dwLength,
			  &dwReadSize,
			  NULL);
	bDone = true;
//Exit0:
	return bDone;
}


bool KFile::MyWriteFile(HANDLE hCFile, DWORD *pdwContext, DWORD dwLength)
{
	bool bDone = false;
	DWORD dwReadSize = 0;
	WriteFile(hCFile,
		pdwContext,
		dwLength,
		&dwReadSize,
		NULL);
	bDone = true;
//Exit0:
	return bDone;
}


bool KFile::MyWriteFile(HANDLE hCFile, FILETIME *pdwContext, DWORD dwLength)
{
	bool bDone = false;
	DWORD dwReadSize = 0;
	WriteFile(hCFile,
		pdwContext,
		dwLength,
		&dwReadSize,
		NULL);
	bDone = true;
//Exit0:
	return bDone;
}


bool KFile::MyDeleteFile(CString szPath)
{
	PROCESS_ERROR(!szPath.IsEmpty());
Exit0:
	return DeleteFile(szPath);
}


bool KFile::MyGetFileInformation(HANDLE hOFile, BY_HANDLE_FILE_INFORMATION &lpFileInformation)
{
	bool bDone = false;
	GetFileInformationByHandle(hOFile, &lpFileInformation);
	bDone = true;
//Exit0:
	return bDone;
}


bool KFile::MySetFileTime(HANDLE hCFile, FILETIME *lpCreationTime, FILETIME *lpLastAccessTime, FILETIME *lpLastWriteTime)
{
	bool bDone = false;
	if (NULL == lpCreationTime || NULL == lpLastAccessTime || NULL == lpLastWriteTime)
	{
		PROCESS_ERROR(false);
	}
	bDone = SetFileTime(hCFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
Exit0:
	return bDone;
}